package com.esd.task;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.esd.config.BaseConfig;
import com.esd.core.CollectionAll;
import com.esd.entity.other.Complaint;
import com.esd.entity.other.Report;
import com.esd.util.Util;

/**
 * 定时任务
 * 
 * @author liuk
 * 
 */
@Component
public class MonitorTask {
	
	private static Logger logger = Logger.getLogger(MonitorTask.class);

	@Resource
	private MongoTemplate mongoTemplate;
	
	@Resource
	private CollectionAll collectionAll;

	@Value("${exportPath}")
	private String exportPath;
	
	@Scheduled(cron = "* * * * * ? ")
	public void timerTask() {
		if (new SimpleDateFormat("HH:mm:ss").format(new Date()).equals(BaseConfig.time)) {
			collectionAll.init(BaseConfig.INDEX_URL[0]);
			collectionAll.start();
		}
	}
	
	/**
	 * 在导出目录初始化一个文件
	 */
	@PostConstruct
	public void init() {
		StringBuilder writer = new StringBuilder();
		writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		try {
			Util.createNewFile(writer.toString(), exportPath + File.separator + "Test.xml");
			logger.debug("初始化导出文件成功:   <" + exportPath.trim() + File.separator + "Test.xml>");
		} catch (IOException e) {
			logger.error("初始化文件发生错误:" + e);
		}
	}
	
	/**
	 * 凌晨1点开始导出
	 * 投诉申诉
	 * 
	 * @throws IOException
	 */
//	@Scheduled(fixedDelay = 100000)
//	@Scheduled(cron = "* * * * * ? ")
	@Scheduled(cron = "0 0 1 * * *")
	public void exportComplaint() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String fileName = "ss" + sdf.format(new Date()) + ".xml";
		StringBuilder writer = new StringBuilder();
		writer.append(appendXml("  <?xml version=\"1.0\" encoding=\"UTF-8\" ?> "));
		writer.append(appendXml("- <!--  申诉数据交换文件 "));
		writer.append(appendXml("  --> "));
		writer.append(appendXml("- <data>"));
		List<Complaint> complaintList = mongoTemplate.find(new Query(Criteria.where("flag").is("0")), Complaint.class);
		for (Complaint complaint : complaintList) {
			writer.append(appendXml("- <row>"));
			writer.append(appendXml("- <queryno>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getId() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </queryno>"));
			writer.append(appendXml("- <!--  查询编号 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <sex>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getGender() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </sex>"));
			writer.append(appendXml("- <!--  性别 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <name>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getName() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </name>"));
			writer.append(appendXml("- <!--  姓名 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <tel>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getPhone() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </tel>"));
			writer.append(appendXml("- <!--  您的联系电话 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <email>"));
			writer.append(appendXml("- <![CDATA[ "));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </email>"));
			writer.append(appendXml("- <prosaddrtype>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getCity() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </prosaddrtype>"));
			writer.append(appendXml("- <!--  被申诉方地址 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <prosaddr>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getLocal() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </prosaddr>"));
			writer.append(appendXml("- <!--  被申诉方详细地址 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <tel2>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getNumber() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </tel2>"));
			writer.append(appendXml("- <!--  被申诉方电话 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <entname>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getDesignation() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </entname>"));
			writer.append(appendXml("- <!--  被申诉方名称 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <goodstype>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getGoods() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </goodstype>"));
			writer.append(appendXml("- <!--  商品/服务 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <goodsname>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getGoods_name() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </goodsname>"));
			writer.append(appendXml("- <!--  商品类别 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <signname>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getBrand() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </signname>"));
			writer.append(appendXml("- <!--  品牌名称 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <model>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getModel() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </model>"));
			writer.append(appendXml("- <!--  型号 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <money>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getPrice() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </money>"));
			writer.append(appendXml("- <!--  价格 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <happdate>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getYear2() + "-" + (complaint.getMonth2().length() > 1 ? complaint.getMonth2() : "0" + complaint.getMonth2()) + "-" + (complaint.getDay2().length() > 1 ? complaint.getDay2() : "0" + complaint.getDay2()) + " 00:00:00"));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </happdate>"));
			writer.append(appendXml("- <!--  发生时间 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <shortsitu>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getContent() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </shortsitu>"));
			writer.append(appendXml("- <!--  简要情况 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <buydate>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getYear() + "-" + (complaint.getMonth().length() > 1 ? complaint.getMonth() : "0" + complaint.getMonth()) + "-" + (complaint.getDay().length() > 1 ? complaint.getDay() : "0" + complaint.getDay()) + " 00:00:00"));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </buydate>"));
			writer.append(appendXml("- <!--  购买时间 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <crtime>"));
			writer.append(appendXml("- <![CDATA[ " + complaint.getDate() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </crtime>"));
			writer.append(appendXml("- <!--  提交时间 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("  </row>"));
			mongoTemplate.updateMulti(new Query(Criteria.where("_id").is(complaint.getId())), Update.update("flag", "1"), Complaint.class);
//			mongoDBDao.update(new BasicDBObject("_id",new ObjectId(complaint.getId())), Update.update("flag", "1"), Complaint.class);
		}
		writer.append(appendXml("  </data>"));
		try {
			logger.debug("投诉导出:   <" + exportPath.trim() + File.separator + fileName + ">");
			Util.createNewFile(writer.toString(), exportPath + File.separator + fileName);
			logger.debug("投诉导出成功");
		} catch (IOException e) {
			logger.error(e);
		}
	}
	
	
	/**
	 * 导出举报
	 */
//	@Scheduled(cron = "* * * * * ? ")
//	@Scheduled(fixedDelay = 600000)
	@Scheduled(cron = "0 0 1 * * *")
	public void exportReport() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String fileName = "jb" + sdf.format(new Date()) + ".xml";
		StringBuilder writer = new StringBuilder();
		writer.append(appendXml("  <?xml version=\"1.0\" encoding=\"UTF-8\" ?> "));
		writer.append(appendXml("- <!--  举报数据交换文件 "));
		writer.append(appendXml("  --> "));
		writer.append(appendXml("- <data>"));
		List<Report> reportList = mongoTemplate.find(new Query(Criteria.where("flag").is("0")), Report.class);
		for (Report report : reportList) {
			writer.append(appendXml("- <row>"));
			writer.append(appendXml("- <queryno>"));
			writer.append(appendXml("- <![CDATA[ " + report.getId() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </queryno>"));
			writer.append(appendXml("- <!--  查询编号 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <sex>"));
			writer.append(appendXml("- <![CDATA[ " + report.getGender() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </sex>"));
			writer.append(appendXml("- <!--  性别 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <name>"));
			writer.append(appendXml("- <![CDATA[ " + report.getName() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </name>"));
			writer.append(appendXml("- <!--  姓名 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <tel>"));
			writer.append(appendXml("- <![CDATA[ " + report.getPhone() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </tel>"));
			writer.append(appendXml("- <!--  您的联系电话 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <email>"));
			writer.append(appendXml("- <![CDATA[ "));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </email>"));
			writer.append(appendXml("- <prosaddrtype>"));
			writer.append(appendXml("- <![CDATA[ " + report.getCity() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </prosaddrtype>"));
			writer.append(appendXml("- <!--  被申诉方地址 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <prosaddr>"));
			writer.append(appendXml("- <![CDATA[ " + report.getLocal() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </prosaddr>"));
			writer.append(appendXml("- <!--  被申诉方详细地址 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <tel2>"));
			writer.append(appendXml("- <![CDATA[ " + report.getNumber() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </tel2>"));
			writer.append(appendXml("- <!--  被申诉方电话 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <entname>"));
			writer.append(appendXml("- <![CDATA[ " + report.getDesignation() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </entname>"));
			writer.append(appendXml("- <!--  被申诉方名称 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <goodstype>"));
			writer.append(appendXml("- <![CDATA[ " + report.getGoods() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </goodstype>"));
			writer.append(appendXml("- <!--  商品/服务 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <goodsname>"));
			writer.append(appendXml("- <![CDATA[ " + report.getGoods_name() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </goodsname>"));
			writer.append(appendXml("- <!--  商品名称 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <shortsitu>"));
			writer.append(appendXml("- <![CDATA[ " + report.getContent() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </shortsitu>"));
			writer.append(appendXml("- <!--  简要情况 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <demand>"));
			writer.append(appendXml("- <![CDATA[ " + (report.getLook_into() != "" ? report.getLook_into() : "") + (report.getInvestigate() != "" ? "," + report.getInvestigate() : "") + (report.getSecrecy() != "" ? "," + report.getSecrecy() : "") + (report.getCommunication() != "" ? "," + report.getCommunication() : "") + (report.getAward() != "" ? "," + report.getAward() : "") + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </demand>"));
			writer.append(appendXml("- <!--  举报人要求 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <happdate>"));
			writer.append(appendXml("- <![CDATA[ " + report.getYear() + "-" + (report.getMonth().length() > 1 ? report.getMonth() : "0" + report.getMonth()) + "-" + (report.getDay().length() > 1 ? report.getDay() : "0" + report.getDay()) + " 00:00:00"));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </happdate>"));
			writer.append(appendXml("- <!--  发生时间 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <password>"));
			writer.append(appendXml("- <![CDATA[ " + report.getPassword() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </password>"));
			writer.append(appendXml("- <!--  设定的密码 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("- <crtime>"));
			writer.append(appendXml("- <![CDATA[ " + report.getDate() + ""));
			writer.append(appendXml("  ]]> "));
			writer.append(appendXml("  </crtime>"));
			writer.append(appendXml("- <!--  提交时间 "));
			writer.append(appendXml("  --> "));
			writer.append(appendXml("  </row>"));
			mongoTemplate.updateMulti(new Query(Criteria.where("_id").is(report.getId())), Update.update("flag", "1"), Report.class);
		}
		writer.append(appendXml("  </data>"));
		try {
			logger.debug("举报导出:   <" + exportPath.trim() + File.separator + fileName + ">");
			Util.createNewFile(writer.toString(), exportPath + File.separator + fileName);
			logger.debug("举报导出成功");
		} catch (IOException e) {
			logger.error(e);
		}
	}
	
	public String appendXml (String xml){
		return xml + "\r\n";
	}
	
}